﻿Imports System.Windows.Forms
Imports System.Data
Imports System.Data.Sql
Imports System.Data.SqlClient
Imports System.Data.OleDb
Imports Microsoft.Win32

Public Class Project_Name

    Public Sub cmb_Select_Import_Product_click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmb_ProjName.Click
        cmb_ProjName.DroppedDown = True
    End Sub

    Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click
        If cmb_ProjName.Text <> "" Then
            'Check if entry exists on table already
            Try
                Dim SQLResult As Object
                Dim Records_Retrieved As String = 0

                ESBD_Form.OpenSQLConnection()
                Dim connection As New SqlConnection(csb.ToString())
                cmd.Connection = connection
                connection.Open()
                cmd.CommandText = "Select Count(*) from Project_TB where PJ_Name = '" + cmb_ProjName.Text + "'"
                SQLResult = cmd.ExecuteScalar
                Records_Retrieved = SQLResult

                If Records_Retrieved = 0 Then
                    connection.Close()
                    Dim decision As DialogResult

                    decision = MsgBox(cmb_ProjName.Text + " does not exist in the database." + vbCrLf + vbCrLf + vbCrLf + "Add " + cmb_ProjName.Text + " to the database?", MsgBoxStyle.Question Or MsgBoxStyle.OkCancel)

                    If decision = DialogResult.OK Then
                        'Ask for password
                        If Admin_PW_Entered = "Y" Then
                            GoTo PW_Accepted
                        Else
                            Dim Passwd As String
                            Passwd = InputBox("Please enter Administrator password.", "Add Items Password")
                            If Passwd = Admin_PW Then
                                Admin_PW_Entered = "Y"
                                EditRec_PW_Entered = "Y"
                                DelRec_PW_Entered = "Y"
                                GoTo PW_Accepted
                            ElseIf Passwd = "" Then
                                GoTo End_Block
                            Else
                                MsgBox("Sorry the password you entered did not match the password on file." + vbCrLf + "Please check spelling and capitalization and try again.", MsgBoxStyle.Information)
                                GoTo End_Block
                            End If
                        End If

PW_Accepted:
                        Add_New_Entry()
                        NewRecord_Form.TB_ProjName.Text = cmb_ProjName.Text
                        Me.DialogResult = System.Windows.Forms.DialogResult.OK
                        Me.Close()
End_Block:
                    Else 'Cancel or window closed
                        'Reload this window
                    End If
                Else
                    'Do Nothing
                    NewRecord_Form.TB_ProjName.Text = cmb_ProjName.Text
                    Me.DialogResult = System.Windows.Forms.DialogResult.OK
                    connection.Close()
                End If
            Catch ex As Exception
                Dim connection As New SqlConnection(csb.ToString())
                connection.Close()
                MsgBox(ex.Message)
            End Try
        Else
            MsgBox("Project Name is required and cannot be blank." + vbCrLf + vbCrLf + "Please enter or select a Project Name.", MsgBoxStyle.Information)
        End If
    End Sub

    Private Sub Add_New_Entry()
        Try
            ESBD_Form.OpenSQLConnection()
            Dim connection As New SqlConnection(csb.ToString())
            cmd.Connection = connection
            connection.Open()

            cmd.CommandText = "INSERT INTO Project_TB ( [PJ_Name] ) VALUES('" + cmb_ProjName.Text + "');"
            cmd.ExecuteScalar()
            connection.Close()
        Catch ex As Exception
            Dim connection As New SqlConnection(csb.ToString())
            connection.Close()
            MsgBox(ex.Message)
        End Try
Finish:
    End Sub

    Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel_Button.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub

    Private Sub Project_Name_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.cmb_ProjName.Select()
    End Sub
End Class
